package org.teamh.connection.json.handler;

import org.teamh.connection.json.event.JsonEventObject;
import org.teamh.game.Start;
import org.teamh.gui.Event;
import org.teamh.logic.datamodel.Creep;
import org.teamh.logic.datamodel.CreepDescription;
import org.teamh.logic.datamodel.DatamodelManager;

public class CreepHandler extends JsonMasterHandler {

	public CreepHandler(Start start, DatamodelManager manager) {
		super(start, manager);
	}

	public boolean handleMsg(JsonEventObject json) {
		
		if (json.getSource().startsWith("Creep@")) {
			if (json.getProperty().equals("cell")) {
				if (json.getNewValue() != null && json.getOldValue() != null) {
					//running
					manager.getCreep(json.getSource()).setCell(manager.getCell(json.getNewValue()));
				}
				if (json.getNewValue() == null && json.getOldValue() != null) {
					Creep creep = manager.getCreep(json.getSource());
					if(creep != null) {
						creep.setCell(null); 
					}
				} else {
					if (manager.getCreep(json.getSource()) == null) {
						Creep creep = new Creep(json.getSource());
						manager.addCreep(creep);
						manager.getCell(json.getNewValue()).addCreep(creep);
						//manager.getCreep(json.getSource()).setCell(manager.getCell(json.getNewValue()));
					}
				}	
				return true;
			} else if (json.getProperty().equals("health")) {
				if(manager.getCreep(json.getSource()) != null) {
					manager.getCreep(json.getSource()).setHealth(Integer.parseInt(json.getNewValue()));
				}
				return true;
			} else if (json.getProperty().equals("slowed")) {
				if(manager.getCreep(json.getSource()) != null) {
					manager.getCreep(json.getSource()).setSlowed(Boolean.parseBoolean(json.getNewValue()));
				}
				return true;
			} else if (json.getProperty().equals("slowTime")) {
				if(manager.getCreep(json.getSource()) != null) {
					manager.getCreep(json.getSource()).setSlowTime(Integer.parseInt(json.getNewValue()));
				}
				return true;
			} else if (json.getProperty().equals("speed")) {
				if(manager.getCreep(json.getSource()) != null) {
					manager.getCreep(json.getSource()).setSpeed(Double.parseDouble(json.getNewValue()));
				}
				return true;
			} else if (json.getProperty().equals("type")) {
				manager.getCreep(json.getSource()).setType(json.getNewValue());
				CreepDescription creepDescription = null;
				for(CreepDescription cd: manager.getCreepDescriptions().values()) {
					if(cd.getType().equals(json.getNewValue())) {
						creepDescription = cd;
						break;
					}
				}
				manager.getCreep(json.getSource()).setCreepDescription(creepDescription);
				return true;
			} else if (json.getProperty().equals("name")) {
				manager.getCreep(json.getSource()).setName(json.getNewValue());
				return true;
			} else if (json.getProperty().equals("userAssets")) {
				if (json.getNewValue() != null) {
					manager.getCreep(json.getSource()).setUserAssets(manager.getUserAssets(json.getNewValue()));
					start.getGui().addEvent(new Event("creepCreated", manager.getCreep(json.getSource()).getID()));
				} 
				if(json.getNewValue() == null && json.getOldValue() != null) {
					Creep creep = manager.getCreep(json.getSource());
					if(creep != null) {
						creep.setUserAssets(null);
					}
				}
				return true;
			} else if (json.getProperty().equals("currentWaypoint")) {
				if (json.getNewValue() != null) {
					manager.getCreep(json.getSource()).setCurrentWaypoint(manager.getWaypoint(json.getNewValue()));
				}
				return true;
			}
		}
		return false;
	}
}
